package com.briup.app03.web.controller;

import javax.validation.constraints.NotNull;

import com.briup.app03.bean.Account;
import com.briup.app03.service.IAccountService;
import com.briup.app03.utils.Message;
import com.briup.app03.utils.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: app03
 * @description: 账户的控制器类
 * @author: charles
 * @create: 2020-12-14 16:20
 **/
@RestController
@RequestMapping("/account")
@Validated
public class AccountController {

    @Autowired
    private IAccountService accountService;

    @GetMapping("findAll")
    public Message findAll(){
        List<Account> list = accountService.findAll();
        return MessageUtil.success(list);
    }

    @GetMapping("findById")
    public Message findById(long id){
        Account account = accountService.findById(id);
        return MessageUtil.success(account);
    }

    @DeleteMapping("deleteById")
    public Message deleteById(long id) {
        accountService.deleteById(id);
        return MessageUtil.success("删除成功！");
    }

    @PostMapping("save")
    public Message save(@NotNull String account ,@NotNull String password){
        Account a = new Account();
        a.setAccount(account);
        a.setPassword(password);

        accountService.save(a);
        return MessageUtil.success("保存成功!");
    }

}
